package util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtils {
    public static String request(String api) throws IOException {
        HttpURLConnection connection = null;
        int responseCode = 0;
        try {
            URL url = new URL(api);
            connection = (HttpURLConnection) url.openConnection();
            responseCode = connection.getResponseCode();

        } catch (Exception e) {
            e.printStackTrace();

        }
        if (200 <= responseCode && responseCode <= 299) {
            try (InputStream is = connection.getInputStream();
                 BufferedReader br = new BufferedReader(new InputStreamReader(is));
            ) {
                StringBuffer response = new StringBuffer();
                String line;
                while ((line = br.readLine()) != null) {
                    response.append(line);
                }
                return response.toString();

            }catch (Exception e){
                e.printStackTrace();
            }


        }
        return null;
    }
}
